<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col>
        <!-- 搜索表单 -->
        <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="分类名" prop="name">
            <el-input v-model="queryParams.name" placeholder="请输入分类名" clearable style="width: 200px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <!-- 操作按钮 -->
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="Plus" @click="handleAdd">新增</el-button>
          </el-col>
        </el-row>

        <!-- 分类表格 -->
        <el-table v-loading="loading" :data="categoryList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="分类名" align="center" prop="name" />
          <el-table-column label="描述" align="center" prop="description" />
          <el-table-column label="创建时间" align="center" prop="createdAt" />
          <el-table-column label="操作" align="center" width="180">
            <template #default="scope">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)">编辑</el-button>
              <el-button link type="danger" icon="Delete" @click="handleDelete(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
      </el-col>
    </el-row>

    <!-- 新增/编辑分类弹窗 -->
    <el-dialog :title="title" v-model="open" width="400px" append-to-body>
      <el-form :model="form" :rules="rules" ref="formRef" label-width="80px">
        <el-form-item label="分类名" prop="name">
          <el-input v-model="form.name" placeholder="请输入分类名" maxlength="50" />
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="form.description" placeholder="请输入描述" maxlength="255" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确定</el-button>
          <el-button @click="cancel">取消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { listCategory, getCategory, addCategory, updateCategory, delCategory } from '@/api/category'
export default {
  data() {
    return {
      queryParams: { pageNum: 1, pageSize: 10, name: '' },
      categoryList: [],
      total: 0,
      loading: false,
      showSearch: true,
      open: false,
      title: '',
      form: {},
      rules: {
        name: [{ required: true, message: '请输入分类名', trigger: 'blur' }]
      },
      selectedRows: []
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.loading = true
      listCategory(this.queryParams).then(res => {
        this.categoryList = res.data.records
        this.total = res.data.total
        this.loading = false
      })
    },
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    resetQuery() {
      this.queryParams = { pageNum: 1, pageSize: 10, name: '' }
      this.getList()
    },
    handleSelectionChange(selection) {
      this.selectedRows = selection
    },
    handleAdd() {
      this.form = {}
      this.open = true
      this.title = '新增分类'
    },
    handleUpdate(row) {
      getCategory(row.id).then(res => {
        this.form = { ...res.data }
        this.open = true
        this.title = '编辑分类'
      })
    },
    handleDelete(row) {
      let id = row ? row.id : (this.selectedRows[0] && this.selectedRows[0].id)
      if (!id) {
        this.$message.warning('请选择要删除的分类')
        return
      }
      this.$confirm('确定要删除选中的分类吗？', '提示', { type: 'warning' }).then(() => {
        delCategory(id).then(() => {
          this.$message.success('删除成功')
          this.getList()
        })
      })
    },
    submitForm() {
      this.$refs.formRef.validate(valid => {
        if (!valid) return
        if (!this.form.id) {
          addCategory(this.form).then(() => {
            this.$message.success('新增成功')
            this.open = false
            this.getList()
          })
        } else {
          updateCategory(this.form).then(() => {
            this.$message.success('修改成功')
            this.open = false
            this.getList()
          })
        }
      })
    },
    cancel() {
      this.open = false
    }
  }
}
</script> 